#include "warninglabel.h"
#include "models/command.h"
#include "qdebug.h"
#include "qpainter.h"
#include <QTimer>

WarningLabel::WarningLabel(QWidget *parent, Qt::WindowFlags f):QLabel(parent,f)
{
    connect(&m_updateTimer,&QTimer::timeout, this, [this](){
        m_nOffset += int(3*g_xScale);
        if (m_nOffset > width() + fontMetrics().width(text())) {
            m_nOffset = 0;
        }
        update();
    });
}

void WarningLabel::setText(const QString & str)
{
    m_nOffset = 0;
    if(str.length() > 0)
    {
        m_updateTimer.start(20);
    }
    else
    {
        m_updateTimer.stop();
    }
    QLabel::setText(str);
}

void WarningLabel::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    // Set the color
    QColor color = Qt::red;
    painter.setPen(color);

    // Draw the text
    QRect rc = rect();
    rc.setLeft(width()-m_nOffset);
    painter.drawText(rc, Qt::AlignVCenter, text());
}
